package com.zhangbo.share.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * Created by zhangbo on 2020/9/7.
 */
public class AopUtils {


    public static Method resolveMethod(ProceedingJoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Class<?> targetClass = joinPoint.getTarget().getClass();
        Method method = getDeclaredMethodFor(targetClass, signature.getName(), signature.getMethod().getParameterTypes());
        if (null != method) {
            return method;
        }
        throw new IllegalStateException("Cannot resolve target method: " + signature.getMethod().getName());
    }

    public static Method getDeclaredMethodFor(Class<?> clazz, String name, Class<?>... parameterTypes) {
        try {
            return clazz.getDeclaredMethod(name, parameterTypes);
        } catch (NoSuchMethodException e) {
            /*当前类没取到，则取父类的*/
            Class<?> superClass = clazz.getSuperclass();
            if (null != superClass) {
                return getDeclaredMethodFor(superClass, name, parameterTypes);
            }
        }
        return null;
    }


}
